package com.jaydenxiao.common.commonutils;

import android.content.Context;

import java.io.UnsupportedEncodingException;

/**
 * Created by Administrator on 2016/6/12.
 */
public class TLHttpUtil {

    /**
     * 加密
     *
     * @param data
     * @return
     */
    public static String e(String data) {
        if (isStrEmpty(data)) {
            return "";
        }
        String e;
        int size = data.length();
        int m;
        char[] chars = new char[size];
        for (int i = 0; i < size; i++) {
            m = ((int) data.charAt(i)) ^ 64;
            chars[i] = (char) m;
        }
        e = new String(chars);
        try {
            byte[] encode = Base64.encodeToByte(e.toString().getBytes("UTF-8"), false);
            e = new String(encode, "UTF-8");
            return e;
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
            return "";
        }
    }

    /**
     * 解密
     *
     * @param data
     * @return
     */
    public static String d(String data) {
        try {
            byte[] decode = Base64.decode(data.toString().getBytes("UTF-8"));
            if (decode == null) {
                return null;
            }
            String body = new String(decode, "UTF-8");
            int size = body.length();
            char[] ch = new char[size];
            int m;
            for (int i = 0; i < size; i++) {
                m = ((int) body.charAt(i)) ^ 64;
                ch[i] = (char) m;
            }
            return new String(ch);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 生成body体提交需要的随机数
     *
     * @param context
     * @return
     */
    public static String r(Context context) {
        StringBuffer r = new StringBuffer();
        String did = DeviceUtil.getDeviceId(context);
//        int size = did.length();
//        r.append(did.substring(size - 5, size));
//        r.append(System.currentTimeMillis());
//        return r.toString();
        return did;
    }

    public static boolean isStrEmpty(String string){
        return string != null && !string.isEmpty() && !"".equals(string) ? false : true;
    }
}
